package calendar;

import java.io.IOException;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;

/* this class is used to provide callback methods to the remote calendars */
public class Callback extends UnicastRemoteObject implements CallbackInt
{
    private static final long serialVersionUID = 345439543L;

    ClientPanel clientPanel;

    public Callback() throws RemoteException
    {
        super();
    }

    /* notify a client of an appointment or a new event */
    public void notifyClientEvent( Event event, boolean appt ) throws IOException
    {
        System.out.println("Callback: notifyClientEvent "+event.desc);
        if ( event != null )
        {
        if ( appt )
        {
            String alert = "The event '" + event.getDesc() + "' is starting now.";
            clientPanel.addAlert( alert );
            clientPanel.showPopup( alert );
        }
        else
        {
            if ( event.getType() == 3 && event.getOwnerId() != clientPanel.getUserId() )
            {
                String alert = "Group event scheduled by "
                + event.getOwner().getName() + ":  '" + event.getDesc()
                + "' starting on " + event.getStart().toString();
                clientPanel.addAlert( alert );
            }
        }
        }
        else
        {
        clientPanel.addAlert( "Error: Null event sent to callback." );
        }
    }

    /* notify a client of a new calendar */
    public void notifyClientUser( User user, ArrayList users ) throws IOException
    {
        System.out.println("Callback: notifyClientUser "+user.getName());
        if ( user != null )
        {
            String alert = "Calendar has been created for new user:  "
            + user.getName();
            clientPanel.addAlert( alert );
            clientPanel.resetUserListPanel( users );
        }
    }

    public void setClientPanel( ClientPanel clientPanel )
    {
        this.clientPanel = clientPanel;
    }
}